using System;
using Microsoft.Win32;
using System.Security.Permissions;
using System.Security;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace BreezeConfig
{
	/// <summary>
	/// Summary description for Form2.
	/// </summary>
	public class Form2 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.OpenFileDialog openFileDialog1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.OpenFileDialog openFileDialog2;
		private System.Windows.Forms.TextBox pscppathBox;
		private System.Windows.Forms.TextBox configpathBox;
		private System.Windows.Forms.Button pscpbrowseButton;
		private System.Windows.Forms.Button configbrowseButton;
		private System.Windows.Forms.Button cancelButton;
		private System.Windows.Forms.Button saveButton;
		public delegate void PassKey(string s);
		public PassKey passKey;

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form2()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form2));
			this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.openFileDialog2 = new System.Windows.Forms.OpenFileDialog();
			this.pscppathBox = new System.Windows.Forms.TextBox();
			this.configpathBox = new System.Windows.Forms.TextBox();
			this.pscpbrowseButton = new System.Windows.Forms.Button();
			this.configbrowseButton = new System.Windows.Forms.Button();
			this.saveButton = new System.Windows.Forms.Button();
			this.cancelButton = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// openFileDialog1
			// 
			this.openFileDialog1.AddExtension = false;
			this.openFileDialog1.DefaultExt = "exe";
			this.openFileDialog1.Filter = "pscp.exe|*.exe|All Files|*.*";
			this.openFileDialog1.RestoreDirectory = true;
			this.openFileDialog1.Title = "Browse...";
			// 
			// label1
			// 
			this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label1.Location = new System.Drawing.Point(16, 16);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(144, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "path to pscp.exe:";
			// 
			// label2
			// 
			this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label2.Location = new System.Drawing.Point(16, 112);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(100, 16);
			this.label2.TabIndex = 1;
			this.label2.Text = "path to config.dat:";
			// 
			// openFileDialog2
			// 
			this.openFileDialog2.AddExtension = false;
			this.openFileDialog2.Filter = "Data Files (*.dat)|*.dat|All Files|*.*";
			this.openFileDialog2.RestoreDirectory = true;
			this.openFileDialog2.Title = "Browse...";
			// 
			// pscppathBox
			// 
			this.pscppathBox.Location = new System.Drawing.Point(16, 32);
			this.pscppathBox.Name = "pscppathBox";
			this.pscppathBox.Size = new System.Drawing.Size(336, 20);
			this.pscppathBox.TabIndex = 2;
			this.pscppathBox.Text = "";
			// 
			// configpathBox
			// 
			this.configpathBox.Location = new System.Drawing.Point(16, 128);
			this.configpathBox.Name = "configpathBox";
			this.configpathBox.Size = new System.Drawing.Size(336, 20);
			this.configpathBox.TabIndex = 3;
			this.configpathBox.Text = "";
			// 
			// pscpbrowseButton
			// 
			this.pscpbrowseButton.Location = new System.Drawing.Point(288, 56);
			this.pscpbrowseButton.Name = "pscpbrowseButton";
			this.pscpbrowseButton.Size = new System.Drawing.Size(56, 16);
			this.pscpbrowseButton.TabIndex = 4;
			this.pscpbrowseButton.Text = "browse";
			this.pscpbrowseButton.Click += new System.EventHandler(this.pscpbrowseButton_Click);
			// 
			// configbrowseButton
			// 
			this.configbrowseButton.Location = new System.Drawing.Point(288, 152);
			this.configbrowseButton.Name = "configbrowseButton";
			this.configbrowseButton.Size = new System.Drawing.Size(56, 16);
			this.configbrowseButton.TabIndex = 5;
			this.configbrowseButton.Text = "browse";
			this.configbrowseButton.Click += new System.EventHandler(this.configbrowseButton_Click);
			// 
			// saveButton
			// 
			this.saveButton.Location = new System.Drawing.Point(272, 200);
			this.saveButton.Name = "saveButton";
			this.saveButton.TabIndex = 6;
			this.saveButton.Text = "Save";
			this.saveButton.Click += new System.EventHandler(this.saveButton_Click);
			// 
			// cancelButton
			// 
			this.cancelButton.Location = new System.Drawing.Point(184, 200);
			this.cancelButton.Name = "cancelButton";
			this.cancelButton.TabIndex = 7;
			this.cancelButton.Text = "Cancel";
			this.cancelButton.Click += new System.EventHandler(this.cancelButton_Click);
			// 
			// Form2
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(368, 243);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.cancelButton,
																		  this.saveButton,
																		  this.configbrowseButton,
																		  this.pscpbrowseButton,
																		  this.configpathBox,
																		  this.pscppathBox,
																		  this.label2,
																		  this.label1});
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Name = "Form2";
			this.Text = "BreezeSCP Advanced Options";
			this.Load += new System.EventHandler(this.Form2_Load);
			this.ResumeLayout(false);

		}
		#endregion

		private void Form2_Load(object sender, System.EventArgs e)
		{
			string keyStr;

			//read current items from reg
			RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\BreezeWorkz\\BreezeSCP");

			if ( key == null )
			{
				MessageBox.Show("Can't locate configuration path.\n\nPlease try reinstalling.\n\n", "Error", MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
				this.pscppathBox.Text = "";
			}

			keyStr = key.GetValue("PuttySCPPath").ToString();
			
			if ( keyStr == null )
			{
				this.pscppathBox.Text = "";
			}
			else
			{
				this.pscppathBox.Text = keyStr.ToString();
			}

			keyStr = key.GetValue("ConfigPath").ToString();
			
			if ( keyStr == null )
			{
				this.configpathBox.Text = "";
			}
			else
			{
				this.configpathBox.Text = keyStr.ToString();
			}
		}

		private void pscpbrowseButton_Click(object sender, System.EventArgs e)
		{
			if ( this.pscppathBox.Text != "" )
				this.openFileDialog1.FileName = this.pscppathBox.Text.ToString();

			this.openFileDialog1.ShowDialog();

			if ( this.openFileDialog1.FileName.ToString() != "" )
				this.pscppathBox.Text = this.openFileDialog1.FileName.ToString();
		}

		private void configbrowseButton_Click(object sender, System.EventArgs e)
		{
			if ( this.configpathBox.Text != "" )
				this.openFileDialog2.FileName = this.configpathBox.Text.ToString();

			this.openFileDialog2.ShowDialog();

			if ( this.openFileDialog2.FileName.ToString() != "" )
				this.configpathBox.Text = this.openFileDialog2.FileName.ToString();
		}

		private void cancelButton_Click(object sender, System.EventArgs e)
		{
			//exit the form
			this.DestroyHandle();
		}

		private void saveButton_Click(object sender, System.EventArgs e)
		{
			if ( passKey != null )
				passKey(this.configpathBox.Text.ToString());

			RegistryKey key = Registry.LocalMachine.CreateSubKey("SOFTWARE\\BreezeWorkz");

			if ( key == null )
			{
				MessageBox.Show("Couldn't open registry key for writing.\n", "Error", MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
			}

			try
			{
				RegistryKey newKey = key.CreateSubKey("BreezeSCP");

				if ( newKey != null )
				{
					newKey.SetValue("ConfigPath", this.configpathBox.Text.ToString());
					newKey.SetValue("PuttySCPPath", this.pscppathBox.Text.ToString());
				}

				newKey.Close();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.ToString());
			}

			key.Close();
			this.DestroyHandle();
		}
	}
}
